import trees
import copy


def crossover(parent1, parent2):
    child = copy.deepcopy(parent1)
    st = trees.getRandomSubtree(parent2)
    trees.insertSubtreeInTree(child, st)

    return child


def mutate(individual,fs,ts):
    mut = trees.generateRandomExpr(fs,ts,3,'grow')
    trees.insertSubtreeInTree(individual, mut)

    return individual


if __name__ == '__main__':
    fs = ['+','-','*','%']
    ts =['x', 1,2]

    gp1 = trees.generateRandomExpr(fs, ts,3,'grow')
    gp2 = trees.generateRandomExpr(fs,ts,3,'grow')

    c1 = crossover(gp1, gp2)

    import textrenderer

    print "gp1   ", gp1
    textrenderer.writeTreeToConsole(gp1)
    print "gp2   ", gp2
    textrenderer.writeTreeToConsole(gp2)
    print "c1    ", c1
    textrenderer.writeTreeToConsole(c1)

    pop = [gp1, gp2]


    if 0:
        import lattice
        import tkrenderer

        labels = lattice.parseTreeToLattice(gp)
        tkrenderer.drawTree(labels)
